module.exports = Behavior({
  properties: { des: { type: Object, value: {}, observer: function (e) {} } },
  data: {
    _imgList: [],
    _videoList: [],
    _downNowTimes: 1,
    _needDownAllTimes: 999,
    _needDownTimes_img: 999,
    _needDownTimes_video: 999,
    _wasDown_img: 0,
    _wasDown_video: 0,
  },
  methods: {
    __init_downloadData: function (e) {
      if (e.length) {
        for (var o = [], i = [], n = 0; n < e.length; n++) {
          var t = e[n];
          'image' === t.type && o.push(t.src),
            'video' === t.type && i.push(t.src);
        }
        this.setData({
          _imgList: o,
          _videoList: i,
          _needDownTimes_img: o.length,
          _needDownTimes_video: i.length,
          _needDownAllTimes: i.length + o.length,
        });
      }
    },
    _down: function () {
      var e = this.data,
        o = e._needDownAllTimes,
        i = e._downNowTimes,
        n = e._needDownTimes_img,
        t = e._needDownTimes_video;
      0 !== n || 0 !== t
        ? (wx.showLoading({
            title: '正在下载'.concat(i, '/').concat(o),
            icon: 'loading',
          }),
          n > 0
            ? (console.log('_needDownTimes_img > 0'), this._downImg())
            : t > 0 &&
              (console.log('_needDownTimes_video > 0'), this._downVideo()))
        : wx.showToast({
            title: '下载完成,请查看相册',
            icon: 'none',
            duration: 2200,
          });
    },
    _downVideo: function () {
      var e = this,
        o = this.data,
        i = o._videoList,
        n = o._downNowTimes,
        t = o._needDownTimes_video,
        s = o._wasDown_video;
      wx.downloadFile({
        url: i[s],
        success: function (o) {
          wx.saveVideoToPhotosAlbum({
            filePath: o.tempFilePath,
            success: function () {
              (e.data._wasDown_video = s + 1),
                (e.data._needDownTimes_video = t - 1),
                (e.data._downNowTimes = n + 1),
                e._down();
            },
            fail: function (e) {
              wx.hideLoading();
            },
          });
        },
        fail: function (e) {
          wx.hideLoading();
        },
      });
    },
    _downImg: function () {
      var e = this,
        o = this.data,
        i = o._imgList,
        n = o._downNowTimes,
        t = o._needDownTimes_img,
        s = o._wasDown_img;
      wx.downloadFile({
        url: i[s],
        success: function (o) {
          wx.saveImageToPhotosAlbum({
            filePath: o.tempFilePath,
            success: function () {
              (e.data._wasDown_img = s + 1),
                (e.data._needDownTimes_img = t - 1),
                (e.data._downNowTimes = n + 1),
                e._down();
            },
            fail: function (e) {
              wx.hideLoading();
            },
          });
        },
        fail: function (e) {
          wx.hideLoading();
        },
      });
    },
    _viewImg: function (e) {
      wx.previewImage({
        current: e.currentTarget.dataset.src,
        urls: this.data._imgList,
      });
    },
    _beforDown: function () {
      var e = this;
      wx.getSetting({
        success: function (o) {
          JSON.stringify(o.authSetting).includes('scope.writePhotosAlbum')
            ? !o.authSetting['scope.writePhotosAlbum'] &&
              JSON.stringify(o.authSetting).includes('scope.writePhotosAlbum')
              ? (wx.showToast({ title: '打开授权才能保存', icon: 'none' }),
                wx.openSetting())
              : 'function' == typeof e._download_afterJudge
                ? e._download_afterJudge()
                : e._down()
            : (wx.showToast({ title: '请同意授权!', icon: 'none' }),
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success: function () {
                  'function' == typeof e._download_afterJudge
                    ? e._download_afterJudge()
                    : e._down();
                },
                fail: function (e) {
                  console.error('authorize 授权失败', e),
                    wx.showToast({ title: '授权失败', icon: 'none' });
                },
              }));
        },
      });
    },
  },
});
